from secret import flag from Crypto.Util.number import * total_bit = 512 defmake_para(bit_size): P = getStrongPrime(bit_size) Q = getStrongPrime(bit_size) N = P * Q magic = (N + 1) * (N + P + Q + 1) + (P - Q) ** 2 + N d = int(0.394 * bit_size) e = inverse(d, magic) k = d * e // magic return (N, e), (P, Q, k) pubkey, sec_key = make_para(2 * total_bit) flag = bytes_to_long(flag) * sec_key[2] p = getStrongPrime(total_bit) q = getStrongPrime(total_bit) n = p * q e = 65537 c = pow(flag, e, n) t = ((p - getRandomNBitInteger(16)) * sec_key[0] + q) % sec_key[1] with open('output.txt', 'w') as f: f.write(str(pubkey) + '\n') f.write(str((t, c)))
from Crypto.Util.number import * p = getPrime(510) q = getPrime(510) n = p * q t = ((p - getRandomNBitInteger(16)) * P + q) % Q shift = 2^1024 m = matrix(ZZ,[[shift*P,1,0,0,0], [shift*1,0,1,0,0], [shift*Q,0,0,1,0], [shift*t,0,0,0,2^512]]) m = m.LLL()
n = 1885106209951408608833065466098355578239648885277085979696889428331716535742564778501798478665957825315340421821880653818505857049636611632357321104069926874970489073929053910350131880591544986024406953378391135673202854750625745159391997973535848495128365477217006260495413869532372418221652962946340513593002422433536479789576519469228846773250447077165756739529520975715667675188738514871033908115371290569902086064227476952606366538782284487477820835988316471
r = 13064264216014600984590013161094647588688097945008015283029781423081403612570218487688473281971441574006356719607329041516297420593832435695152551495621303
# n = p * q * r # mod = myfunction(3) # print(mod==n**3)
from secret import flag, genPrime, generate_g import random import math import os
sieve_base = # from Crypto.Util.number import sieve_base
defrabinMillerTest(n, rounds): tested = [] if n < 3or (n & 1) == 0: return n == 2, tested # n might be very large so it might be beneficial to precalculate n-1 n_1 = n - 1 # determine m and b so that 2**b * m = n - 1 and b maximal b = 0 m = n_1 while (m & 1) == 0: b += 1 m >>= 1 #a smaller number might be faster... upper = 2**(max(n.bit_length() >> 5, 10)) # we need to do at most n-2 rounds. for i in range(min(rounds, n - 2)): a = random.randint(2, upper) while a in tested: a = random.randint(2, upper) tested.append(a) # do the rabin-miller test z = pow(a, m, n) # (a**m) % n if z == 1or z == n_1: continue composite = 1 for r in range(b): z = (z * z) % n if z == 1: return0, tested elif z == n_1: composite = 0 break if composite: return0, tested return1, tested
defis_prime(N, false_positive_prob=1e-6): tested = [] if N < 3or N & 1 == 0: return N == 2, [] for p in sieve_base: if N == p: return1, tested if N % p == 0: tested.append(p) return0, tested
print("Welcome to my secure key exchange system\n") whileTrue: q = int(input("q:")) if q.bit_length() < 256: print("oh! too small!!!") continue jud, tested = is_prime(q) ifnot jud: print(f"Bad parameters! test set {tested} not pass.") else: break
x = int.from_bytes(os.urandom(q.bit_length() // 8), byteorder='big') p = genPrime(q) g = generate_g(q, p) print(f"p: {p}") print(f"g: {g}") A = pow(g, x, p) print(f"Calculate the public key in this way: pow(g, secret, p)") print(f"eg. pow(g, x, p) = {A}") print(f"I can give you a hint:)") r = int(input("r:")) if r < x and ((p - 1) % r == 0): print(pow(g, x, r)) else: print("don't need hint?")
for i in range(100): B = int(input("give me your public key:")) if1 < B < p: shared = pow(B, x, p) print(f"our shared key: {shared}") x_ = int(input("x?").strip()) if x_ == x: print(flag) else: print("sorry~")
defgenPrime(bit_len): whileTrue: x = 2 while x < 2**bit_len: x *= random.choice(sieve_base) if isPrime(x+1) and x.bit_length() % 8 == 0: return x+1 print(genPrime(256))